package com.sn.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sn.shop.dao.CategoryMapper;
import com.sn.shop.model.Category;
import com.sn.shop.model.CategoryTree;
import com.sn.shop.model.Paging;
import com.sn.shop.request.RequestSearchCategory;
import com.sn.shop.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther sn
 * @date 2018/2/8
 * @description TODO
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    public PageInfo<Category> findAllList(RequestSearchCategory category, Paging paging) {
        PageHelper.startPage(paging.getPageNum(), paging.getPageSize(), paging.getOrder());
        List<Category> list = categoryMapper.findAllList(category);
        PageInfo page = new PageInfo(list);
        return page;
    }

    @Override
    public CategoryTree selectTree() {
        CategoryTree tree = new CategoryTree();
        tree.categoryList(categoryMapper.selectList());
        return tree;
    }


    @Override
    public int add(Category category) {
        return categoryMapper.insert(category);
    }

    @Override
    public int batchDelete(String params) {
        String[] tmp = params.split(",");
        List<Integer> data = new ArrayList<>();
        for (String s : tmp) {
            data.add(Integer.parseInt(s));
        }
        return categoryMapper.batchDelete(data);
    }
}
